package com.zuoban.controller;

import com.zuoban.common.Result;
import com.zuoban.constant.HttpStatus;
import com.zuoban.storage.api.StorageApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(tags = "文件管理")
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {

    private final StorageApi storageApi;

    @ApiOperation("文件上传")
    @GetMapping("/upload")
    public Result upload(@RequestParam("file")MultipartFile file) throws IOException {
        String path = storageApi.getNewPath(file.getOriginalFilename());
        String url = storageApi.upload(file.getInputStream(), path);
        return new Result(HttpStatus.HTTP_OK,"上传成功",url);
    }
}
